package com.haixee.parkdemo.auth.controller;

import com.haixee.parkdemo.auth.entity.dto.UserDTO;
import com.haixee.parkdemo.auth.entity.request.OrganizeRequest;
import com.haixee.parkdemo.auth.entity.vo.OrganizeTreeVO;
import com.haixee.parkdemo.auth.enums.OrganizeType;
import com.haixee.parkdemo.auth.enums.TreeTag;
import com.haixee.parkdemo.auth.enums.UserType;
import com.haixee.parkdemo.auth.model.Organize;
import com.haixee.parkdemo.auth.model.Tenant;
import com.haixee.parkdemo.auth.service.OrganizeService;
import com.haixee.parkdemo.auth.service.TenantService;
import com.haixee.parkdemo.auth.service.UserService;
import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.common.response.ResUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "组织", description = "组织管理")
@RestController
@RequestMapping("organize")
public class OrganizeController {

    @Autowired
    private OrganizeService organizeService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    @Operation(summary = "添加组织", description = "添加组织")
    public ResResult add(@RequestBody OrganizeRequest request) {

        organizeService.addOrganize(request.getTenantId(), request.getParentId(), OrganizeType.DEPT.getCode(), request.getName(), request.getIsManage());
        return ResUtil.success();
    }

    @PostMapping("/update")
    public ResResult update(@RequestBody OrganizeRequest request) {

        organizeService.updateOrganize(request.getId(),  request.getName());
        return ResUtil.success();
    }

    @GetMapping("/all")
    public ResResult getTree() {

        Integer tenantId = WebContextHolder.getAuth().getTenantId();


        List<Organize> organizeList = organizeService.getAll(tenantId);
//        List<UserDTO> userList = userService.getAllWithJob(tenantId);

        List<OrganizeTreeVO> unmountList = new ArrayList<>();

        Map<String, OrganizeTreeVO> map = new HashMap<>();
        OrganizeTreeVO theVO = newTenant(tenantId, null, map);


        if (tenantId == 0) {
            List<Tenant> tenants = tenantService.selectAll();
            for (Tenant tenant : tenants) {
                map.get("0ten0").getChildren().add(newTenant(tenant.getId(), tenant, map));
            }
        }


        for (Organize organize : organizeList) {
            OrganizeTreeVO vo = new OrganizeTreeVO();

            if (organize.getType() == OrganizeType.JOB.getCode()) {
                continue;
            }

            vo.setId(organize.getTenantId() + "org" +  organize.getId());
            vo.setOrganizeId(organize.getId());
            vo.setType(organize.getType());
            vo.setParentId(organize.getParentId());
            vo.setName(organize.getName());
            vo.setTenantId(organize.getTenantId());
            map.put(vo.getId(), vo);

            OrganizeTreeVO parentVO = map.get(organize.getTenantId() + "org" + organize.getParentId());
            if (parentVO != null) {
                List<OrganizeTreeVO> children = parentVO.getChildren();
                children.add(vo);
            } else {
                unmountList.add(vo);
            }
        }

        for (OrganizeTreeVO organizeTreeVO : unmountList) {
            OrganizeTreeVO parentVO = map.get(organizeTreeVO.getTenantId() + "org" + organizeTreeVO.getParentId());
            parentVO.getChildren().add(parentVO);
        }

        if (tenantId == 0) {
            return ResUtil.success(map.get("0ten").getChildren());
        } else {
            return ResUtil.success(theVO.getChildren());
        }
    }



    private OrganizeTreeVO newTenant(Integer tenantId, Tenant tenant, Map<String, OrganizeTreeVO> map) {


        OrganizeTreeVO theVO = new OrganizeTreeVO();
        theVO.setName(tenant == null? "": tenant.getName());
        theVO.setTenantId(tenantId);

        if (tenantId == null || tenantId == 0) {
            map.put(tenantId + "ten", theVO);
        } else {
            map.put(tenantId + "org0", theVO);
        }


        if (tenantId == null || tenantId == 0) {
            OrganizeTreeVO theDVO = new OrganizeTreeVO();
            theDVO.setId(tenantId + "org0");
            theDVO.setName("部门");
            theDVO.setTenantId(tenantId);
            theDVO.setIsShowType(true);
            theDVO.setShowType(TreeTag.DEPT.getCode());
            theVO.getChildren().add(theDVO);
            map.put(theDVO.getId(), theDVO);
        }

        if (tenantId == null || tenantId == 0) {
            OrganizeTreeVO theTVO = new OrganizeTreeVO();
            theTVO.setId(tenantId + "ten0");
            theTVO.setName("园区(租户)");
            theTVO.setTenantId(tenantId);
            theTVO.setIsShowType(true);
            theTVO.setShowType(TreeTag.TENANT.getCode());
            theVO.getChildren().add(theTVO);
            map.put(theTVO.getId(), theTVO);
        }

        return theVO;
    }
}
